package com.slz3.business.module.orderManagement.ordOrderLine.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooBrand.domain.GooBrand;
import com.slz3.business.module.goodsManagement.gooGoods.domain.GooGoods;
import com.slz3.business.module.orderManagement.ordOrderLine.domain.OrdOrderLine;
import com.slz3.business.module.orderManagement.ordOrderLine.domain.OrdOrderLineFactory;
import com.slz3.business.module.orderManagement.ordOrderLine.steward.OrdOrderLineSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class OrdOrderLineServiceImpl implements OrdOrderLineService {

    @Override
    public OrdOrderLine save(OrdOrderLine ordOrderLine) {
        return Repositorys.ordOrderLine.save(OrdOrderLineFactory.initBaseFields(ordOrderLine));
    }

    @Override
    public Page<OrdOrderLine> page(MultiValueMap<String, String> params) {
        Page<OrdOrderLine> ordOrderLinePage = Repositorys.ordOrderLine.findAll(
                OrdOrderLineSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return ordOrderLinePage;
    }

    @Override
    public OrdOrderLine modify(OrdOrderLine ordOrderLineNew) {
        return Repositorys.ordOrderLine.saveAndFlush(OrdOrderLineFactory.initBaseFields(ordOrderLineNew));
    }

    @Override
    public Optional<OrdOrderLine> single(UUID uuid) {
        return Repositorys.ordOrderLine.findById(uuid);
    }

    @Override
    public List<OrdOrderLine> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.ordOrderLine.findAllById(uuids);
    }

    @Override
    public List<OrdOrderLine> delete(List<OrdOrderLine> ordOrderLineList) {
        ordOrderLineList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            OrdOrderLineFactory.initBaseFields(item);
        });
        return Services.ordOrderLineTransactional.batchModify(ordOrderLineList);
    }

    @Override
    public List<OrdOrderLine> findBySerOrderId(UUID serOrderId) {
        List<OrdOrderLine> ordOrderLineList = Repositorys.ordOrderLine.findBySerOrderId(serOrderId);

        //封装商品及品牌信息
        ordOrderLineList.stream().forEach(ordOrderLine -> {
            GooGoods gooGoods = new GooGoods();
            GooBrand gooBrand = new GooBrand();
            if(VE.isNotEmpty(ordOrderLine.getSerItemId())){
                gooGoods = Services.gooGoods.single(ordOrderLine.getSerItemId()).orElse(new GooGoods());
                if(VE.isNotEmpty(gooGoods.getSerGoodsBarndId())){
                    gooBrand = Services.gooBrand.single(gooGoods.getSerGoodsBarndId()).orElse(new GooBrand());
                }
            }
            gooGoods.setGooBrand(gooBrand);
            ordOrderLine.setGooGoods(gooGoods);
        });
        return ordOrderLineList;
    }

}
